package com.mm.googleauth.controller;

import com.dingtalk.api.response.OapiUserGetResponse;
import com.mm.googleauth.service.DingdingService;
import com.mm.googleauth.util.GoogleAuth;
import com.warrenstrange.googleauth.GoogleAuthenticator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dd")
public class DingdingController {

    private Logger log = LoggerFactory.getLogger(DingdingController.class);

    @Autowired
    private DingdingService dingdingService;

    @GetMapping("/code/{id}")
    public Map<String, Object> code(@PathVariable("id") String id) {
        log.info("请求的code:{}", id);

        String s = dingdingService.userId(id);
        OapiUserGetResponse response = dingdingService.userInfo(s);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);

        if (response == null) {
            log.error("用户登陆失败");
            map.put("code", 1);
            return map;
        }

        GoogleAuth auth = new GoogleAuth();
        GoogleAuthenticator gAuth = new GoogleAuthenticator();
        int code = gAuth.getTotpPassword(auth.getCode());

        map.put("auth_code", code);
        log.info("返回信息: {}", map);
        return map;
    }


}
